package com.skyline.attraction.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.skyline.attraction.dao.AttractionRefrenceDao;
import com.skyline.attraction.mapper.AttractionRefrenceMapper;
import com.skyline.attraction.model.AttractionRefrence;
import com.skyline.attraction.type.AttractionRefrenceType;
import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.base.type.Activity;
import com.skyline.common.bean.Page;
import com.skyline.wo.mapper.AlbumMapper;
import com.skyline.wo.mapper.ArticleMapper;
import com.skyline.wo.model.Album;
import com.skyline.wo.model.Article;

@Repository("attractionRefrenceDao")
public class AttractionRefrenceDaoImpl extends BaseDaoImpl implements AttractionRefrenceDao {

	/*
	 * insert
	 */
	/**
	 * insert into attractionrefrence
	 * (attractionId,attractionName,attractionPortrait
	 * ,refId,refPortrait,refTitle,refDigest
	 * ,refType,providerId,providerNikename,
	 * providerPortrait,providerEmail,createTime,updateTime) values
	 * (?,?,?,?,?,?,?,?,?,?,?,?,current_timestamp,current_timestamp)
	 */
	@Value("${AttractionFeatureDao.insertRefrence}")
	private String insertRefrenceSql;

	/*
	 * query
	 */
	/**
	 * select * from attractionrefrence where attractionId=? and activity=?
	 */
	@Value("${AttractionFeatureDao.queryRefrencesByAttractionId}")
	private String queryRefrencesByAttractionIdSql;

	/**
	 * select * from attractionrefrence where attractionId=? and refType=? and
	 * activity=?
	 */
	@Value("${AttractionFeatureDao.queryRefrencesByAttractionIdAndType}")
	private String queryRefrencesByAttractionIdAndTypeSql;

	@Value("${AttractionFeatureDao.queryRefrenceById}")
	private String queryRefrenceByIdSql;

	/**
	 * update attractionrefrence set activity=? where id=?
	 */
	@Value("${AttractionFeatureDao.updateRefrenceActivity}")
	private String updateRefrenceActivitySql;

	/**
	 * update attractionrefrence set refPortrait=? where id=?
	 */
	@Value("${AttractionFeatureDao.updateRefPortrait}")
	private String updateRefPortraitSql;

	/**
	 * select count(1) from attractionrefrence where attractionId=? and refType=?
	 * and refId=? and activity=?
	 */
	@Value("${AttractionFeatureDao.countTargetRefrenceOfAttraction}")
	private String countTargetRefrenceOfAttractionSql;

	/**
	 * select t4.*,case when t3.t2act='NORMAL' then true else false end as refed
	 * from article t4 left join (select t1.*,t2.activity t2act from article
	 * t1,attractionrefrence t2 where t1.id=t2.refId and
	 * t2.refType='TRAVELNOTE') t3 on t3.id=t4.id where t4.ownerId=? and
	 * t4.activity=? and t4.authority<=?
	 */
	@Value("${AttractionFeatureDao.queryArticlesWithRefed}")
	private String queryArticlesWithRefedSql;

	/**
	 * select t4.*,case when t3.t2act='NORMAL' then true else false end as refed
	 * from album t4 left join (select t1.*,t2.activity t2act from album
	 * t1,attractionrefrence t2 where t1.id=t2.refId and t2.refType='ALBUM') t3
	 * on t3.id=t4.id where t4.ownerId=? and t4.activity=? and t4.authority<=?
	 */
	@Value("${AttractionFeatureDao.queryAlbumsWithRefed}")
	private String queryAlbumsWithRefedSql;

	/**
	 * select * from attractionrefrence where attractionId=? and activity=? limit
	 * ?
	 */
	@Value("${AttractionFeatureDao.querySomeRefrencesOfAttraction}")
	private String querySomeRefrencesOfAttractionSql;

	/**
	 * select * from attractionrefrence where attractionId=? and refType=? and
	 * activity=? limit ?
	 */
	@Value("${AttractionFeatureDao.querySomeRefrencesOfAttractionWithType}")
	private String querySomeRefrencesOfAttractionWithTypeSql;

	@Override
	public Long insertRefrence(Long attractionId, String attractionName, String attractionPortrait, Long refId,
			String refPortrait, String refTitle, String refDigest, AttractionRefrenceType type, Long providerId,
			String providerNickname, String providerPortrait, String providerEmail) {

		return this.insertWithIdReturn(insertRefrenceSql, attractionId, attractionName, attractionPortrait, refId,
				refPortrait, refTitle, refDigest, type.toString(), providerId, providerNickname, providerPortrait,
				providerEmail);

	}

	/**************************** query **************************/
	@Override
	public List<AttractionRefrence> queryRefrencesByAttractionId(Long attractionId, Activity activity, Page page) {
		List<AttractionRefrence> list = this.getPaginationResult(queryRefrencesByAttractionIdSql, page,
				AttractionRefrenceMapper.getMapper(), attractionId, activity.toString());
		return list;

	}

	@Override
	public List<AttractionRefrence> queryRefrencesByAttractionIdAndType(Long attractionId, AttractionRefrenceType type,
			Activity activity, Page page) {
		return this.getPaginationResult(queryRefrencesByAttractionIdAndTypeSql, page,
				AttractionRefrenceMapper.getMapper(), attractionId, type.toString(), activity.toString());

	}

	@Override
	public AttractionRefrence queryRefrenceById(Long id, Activity activity) {
		List<AttractionRefrence> list = jdbcTemplate.query(queryRefrenceByIdSql, AttractionRefrenceMapper.getMapper(),
				id, activity.toString());
		if (CollectionUtils.isEmpty(list)) {
			return null;
		} else {
			return list.get(0);
		}
	}

	@Override
	public void updateRefrenceActivity(Long id, Activity activity) {
		jdbcTemplate.update(updateRefrenceActivitySql, activity.toString(), id);
	}

	@Override
	public void updateRefPortrait(Long id, String portrait) {
		jdbcTemplate.update(updateRefPortraitSql, portrait, id);
	}

	@Override
	public Integer countTargetRefrenceOfAttraction(Long attractionId, AttractionRefrenceType type, Long refId,
			Activity activity) {
		return jdbcTemplate.queryForInt(countTargetRefrenceOfAttractionSql, attractionId, type.toString(), refId, activity.toString());
	}

	@Override
	public List<Article> queryArticlesWithRefed(Long ownerId, Long attractionId, Activity activity, Integer authority,
			Page page) {
		return getPaginationResult(queryArticlesWithRefedSql, page, ArticleMapper.getMapper(), attractionId, ownerId,
				activity.toString(), authority);
	}

	@Override
	public List<Album> queryAlbumsWithRefed(Long ownerId, Long attractionId, Activity activity, Integer authority,
			Page page) {
		return getPaginationResult(queryAlbumsWithRefedSql, page, AlbumMapper.getMapper(), attractionId, ownerId,
				activity.toString(), authority);
	}

	@Override
	public List<AttractionRefrence> querySomeRefrencesOfAttraction(Long attractionId, Activity activity, Integer size) {
		return jdbcTemplate.query(querySomeRefrencesOfAttractionSql, AttractionRefrenceMapper.getMapper(),
				attractionId, activity.toString(), size);
	}

	@Override
	public List<AttractionRefrence> querySomeRefrencesOfAttractionWithType(Long attractionId,
			AttractionRefrenceType type, Activity activity, Integer size) {
		return jdbcTemplate.query(querySomeRefrencesOfAttractionWithTypeSql, AttractionRefrenceMapper.getMapper(),
				attractionId, type.toString(), activity.toString(), size);
	}
}
